package com.timeste.lasttime;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "LastTimeManager";

	// Contacts table name
	private static final String TABLE_LASTTIME = "lasttime";

	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_TITLE = "title";
	private static final String KEY_DESCRIPTION = "description";
	private static final String KEY_MONTH = "month";
	private static final String KEY_DAY = "day";
	private static final String KEY_YEAR = "year";

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_LASTTIME + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
				+ KEY_DESCRIPTION + " TEXT,"
				+ KEY_YEAR + " INT,"
				+ KEY_MONTH + " INT,"
				+ KEY_DAY + " INT"+ ")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//		// Drop older table if existed
//		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LASTTIME);
//
//		// Create tables again
//		onCreate(db);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */
	
	public void deleteAndCreate() {
		SQLiteDatabase db = this.getReadableDatabase();
		
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_LASTTIME);

		// Create tables again
		onCreate(db);
	}

	// Adding new contact
	void addEvent(LastTimeEvent event) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, event.get_title());
		values.put(KEY_DESCRIPTION, event.get_description());
		values.put(KEY_YEAR, event.get_year());
		values.put(KEY_MONTH, event.get_month());
		values.put(KEY_DAY, event.get_day());

		// Inserting Row
		db.insert(TABLE_LASTTIME, null, values);
		db.close(); // Closing database connection
	}

	// Getting single event
	LastTimeEvent getEvent(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_LASTTIME, new String[] { KEY_ID,
				KEY_TITLE, KEY_DESCRIPTION, KEY_YEAR, KEY_MONTH, KEY_DAY }, KEY_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		LastTimeEvent event = new LastTimeEvent(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), Integer.parseInt(cursor.getString(3)),
				Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
		return event;
	}
	
	// Getting All Contacts
	public List<LastTimeEvent> getAllEvents() {
		List<LastTimeEvent> eventList = new ArrayList<LastTimeEvent>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_LASTTIME;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				LastTimeEvent event = new LastTimeEvent();
				event.set_id(Integer.parseInt(cursor.getString(0)));
				event.set_title(cursor.getString(1));
				event.set_description(cursor.getString(2));
				event.set_year(Integer.parseInt(cursor.getString(3)));
				event.set_month(Integer.parseInt(cursor.getString(4)));
				event.set_day(Integer.parseInt(cursor.getString(5)));
				// Adding contact to list
				eventList.add(event);
				Log.i("INFO Event ", event.toString());
			} while (cursor.moveToNext());
		}

		// return contact list
		return eventList;
	}

//	// Updating single contact
//	public int updateContact(Contact contact) {
//		SQLiteDatabase db = this.getWritableDatabase();
//
//		ContentValues values = new ContentValues();
//		values.put(KEY_NAME, contact.getName());
//		values.put(KEY_PH_NO, contact.getPhoneNumber());
//
//		// updating row
//		return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
//				new String[] { String.valueOf(contact.getID()) });
//	}
//
	// Deleting single contact
	public void deleteContact(LastTimeEvent event) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_LASTTIME, KEY_ID + " = ?",
				new String[] { String.valueOf(event.get_id()) });
		db.close();
	}
//

//	// Getting status Count
//	public int getStatusesCount() {
//		String countQuery = "SELECT  * FROM " + TABLE_LASTTIME;
//		SQLiteDatabase db = this.getReadableDatabase();
//		Cursor cursor = db.rawQuery(countQuery, null);
//		cursor.close();
//
//		// return count
//		return cursor.getCount();
//	}

}
